package com.zf.yichat.api.controller.carbonForest;

import com.zf.yichat.api.controller.BaseController;
import com.zf.yichat.api.dto.request.MallOrderRequest;
import com.zf.yichat.model.SysDict;
import com.zf.yichat.model.UserPrintTreeTask;
import com.zf.yichat.service.BalanceService;
import com.zf.yichat.service.PlantTreeService;
import com.zf.yichat.service.StMallOrderService;
import com.zf.yichat.service.SysDictService;
import com.zf.yichat.utils.response.FsResponse;
import com.zf.yichat.utils.response.FsResponseGen;
import com.zf.yichat.vo.DictKey;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * 碳汇林-商城
 */
@RestController
@RequestMapping("order")
@Slf4j
public class StMallOrderController extends BaseController {


    @Autowired
    private StMallOrderService stMallOrderService;

    @Autowired
    private BalanceService balanceService;
    @Autowired
    private PlantTreeService plantTreeService;

    @Autowired
    private SysDictService sysDictService;
    /**
     * 商城订单支付
     * @return
     */
    @PostMapping("payment")
    public FsResponse payment(@RequestBody MallOrderRequest request) {
        Long userId = getUserId();
        if (null == userId) {
            return FsResponseGen.failMsg("参数错误【userId】");
        }

        // 支付密码
        String password = request.getPassword();
        if (StringUtils.isBlank(password)) {
            return FsResponseGen.failMsg("支付密码参数错误！");
        }

        BigDecimal payAmount = request.getPayAmount();
        if (null == payAmount) {
            return FsResponseGen.failMsg("支付金额参数错误！");
        }

        // 校验支付密码
        balanceService.checkPassword(userId, password);


        // 查询当天已赠送次数
        Integer giftedNum = plantTreeService.queryGiveNumbersByType(getUserId(), UserPrintTreeTask.ORDER_TASK);
        String plantNum = "5";
        SysDict sysDict = sysDictService.selectOne(DictKey.user_order_give_plant_num);
        if (null != sysDict) {
            plantNum = sysDict.getValue();
        }

        // 大于设置的赠送值就不再赠送
        if (giftedNum <= Integer.parseInt(plantNum)) {
            // 如果大于等于100元就送
            BigDecimal limitation = new BigDecimal(100);

            if (payAmount.compareTo(limitation) >= 0) {
                plantTreeService.addTask(getUserId(), "购物满100元", UserPrintTreeTask.ORDER_TASK, 1);
            }
        }

        String orderNo = request.getOrderNo();
        BigDecimal discountsAmount = request.getDiscountsAmount();
        String emsType = request.getEmsType();
        Integer goodsCount = request.getGoodsCount();
        Integer goodsId = request.getGoodsId();
        BigDecimal orderAmount = request.getOrderAmount();

        return stMallOrderService.payment(userId, orderNo, orderAmount, payAmount, discountsAmount,
                goodsId, goodsCount, emsType);
    }

}
